package com.ericsson.enap.modules.ebmLog.task;

import com.ericsson.common.pojo.FTPInfoVO;
import com.ericsson.common.util.*;
import org.slf4j.Logger;

import java.io.File;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @description: ebm文件下载任务多线程任务类
 * @author: wangjingshi
 * @create: 2021-04-19 16:38
 */
public class EbmDownLoadTask implements Runnable{

    private Logger log = LoggerUtils.Logger(LogFileName.VgcEbmLogJob);
    private String netId;
    private FTPInfoVO ftpInfo;
    private String remotePath;
    private String localPath;
    private CountDownLatch latch;
    private int delay;

    public EbmDownLoadTask(FTPInfoVO ftpInfo, String remotePath, String localPath,CountDownLatch latch, int delay) {
        this.netId = String.valueOf(ftpInfo.getNetElementID());;
        this.ftpInfo = ftpInfo;
        this.remotePath = remotePath;
        this.localPath = localPath;
        this.latch = latch;
        this.delay = delay;
    }

    @Override
    public void run() {
        log.info("========= downLoadFiles netId:"+netId+" start=========");
        try {
            this.getFilesFromFTP(ftpInfo, DateUtils.getEbmLogStartMinTime(15+delay), DateUtils.getEbmLogStartMinTime(delay),remotePath,netId);
        } catch (Exception e) {
            log.error("==== EbmDownLoadTask run exception: "+e.getMessage(),e);
        } finally {
            latch.countDown();
        }
        log.info("========= downLoadFiles netId:"+netId+" end=========");
    }

    /**
     * 从网元下载文件
     * @param ftpInfo
     * @param startTime		时间格式  A20150729.0845
     * @param endTime		时间格式  A20150729.0845
     * @param remotePath  EBMLOG服务器上的路径
     * @throws Exception
     * @author wangjingshi
     * @created 2018-9-5 下午1:24:34
     */
    public String getFilesFromFTP(FTPInfoVO ftpInfo, String startTime,String endTime,String remotePath,String netId) throws Exception {
        SFtpCarrierUtil sFtpCarrierUtil = new SFtpCarrierUtil(ftpInfo, log);
        //log.info("=========delete dir start=========");
        FileUtil.deleteDir(new File(localPath));
        //log.info("=========delete dir end=========");
        //log.info("=========download file start=========");
        System.out.println("=========== host:"+ftpInfo.getHost());
        System.out.println("=========== remotePath:"+remotePath);
        System.out.println("=========== localPath:"+localPath);
        List<String> fileNameList = sFtpCarrierUtil.batchDownLoadFile(remotePath,localPath, startTime,endTime, false,netId);
        //scp下载，根据现场情况看用那种下载  青岛scp快
        //List<String> fileNameList = sFtpCarrierUtil.batchDownLoadFileSCP(ftpPath,localPath, startTime,endTime, false,netId);
        log.info("=========download file end total: "+fileNameList.size()+" ====== " + netId);
        return startTime;
    }



}
